An arraylist is basically what it sounds like--a list that is modeled somewhat like an array. I called my arraylist "Epic Array List" because there is an actual ArrayList in Java, and the one I created is slightly different
import java.util.*;
public class EpicArrayList
{
private Object[] objects; Creates an array of objects
private final int DEFAULT_CAPACITY = 10; Default amount of objects the list CAN hold
private int listSize; How many objects the list IS holding
public EpicArrayList() Default Constructor
{
objects[DEFAULT_CAPACITY] = new Object[DEFAULT_CAPACITY];
listSize = 0; Creates the array with the default amount of space
}
public EpicArrayList(int capacity) Constructor that creates a list with a given amount of space
{
objects = new Object[capacity];
listSize = 0;
}
public int listSize() method that returns the size of the list
{
return listSize;
}
public boolean isEmpty()
{
if( listSize == 0 ) return true if there are zero objects in the list
return true;
return false; return false if the list actually has objects in it
}
public void add(int slot, Object obj) inserts an object before the i-th element of the list
{
Object temp[];
if( listSize == objects.length )
{
temp = new Object[ objects.length * 2 ]; if the list is already full, double its capacity
}
else
{
temp = new Object[ objects.length ];
make a temporary array w/ the same capacity
}
for(int i = 0; i < listSize + 1; i++)
{
if( i == slot )
temp[i] = obj;
if( i < slot )
temp[i] = objects[i];
if( i > slot )
temp[i] = objects[i - 1];
}
listSize++;
objects = temp;
}
public Object set(int i, Object obj) Sets the ith spot in an arraylist to a given Object, obj
{
Object temp = objects[i];
objects[i] = obj;
return temp;
}
public Object getItem(int i) Returns the object in spot i of the arraylist
{
return objects[i];
}
public Object remove(int slot) //Removes the object in spot i of the arraylist and shifts all the elements after i back a space
{
Object temp[] = new Object[ objects.length ];
Object returnItem = objects[slot];
for(int i = 0; i < listSize - 1; i++)
{
if( i < slot )
temp[i] = objects[i];
if( i >= slot )
temp[i] = objects[i + 1];
}
objects = temp;
listSize--;
return returnItem;
}
}
import java.util.*;
public class EpicArrayList
{
private Object[] objects; Creates an array of objects
private final int DEFAULT_CAPACITY = 10; Default amount of objects the list CAN hold
private int listSize; How many objects the list IS holding
public EpicArrayList() Default Constructor
{
objects[DEFAULT_CAPACITY] = new Object[DEFAULT_CAPACITY];
listSize = 0; Creates the array with the default amount of space
}
public EpicArrayList(int capacity) Constructor that creates a list with a given amount of space
{
objects = new Object[capacity];
listSize = 0;
}
public int listSize() method that returns the size of the list
{
return listSize;
}
public boolean isEmpty()
{
if( listSize == 0 ) return true if there are zero objects in the list
return true;
return false; return false if the list actually has objects in it
}
public void add(int slot, Object obj) inserts an object before the i-th element of the list
{
Object temp[];
if( listSize == objects.length )
{
temp = new Object[ objects.length * 2 ];
if the list is already full, double its capacity
}
else
{
temp = new Object[ objects.length ];
make a temporary array w/ the same capacity
}
for(int i = 0; i < listSize + 1; i++)
{
if( i == slot )
temp[i] = obj;
if( i < slot )
temp[i] = objects[i];
if( i > slot )
temp[i] = objects[i - 1];
}
listSize++;
objects = temp;
}
public Object set(int i, Object obj) Sets the ith spot in an arraylist to a given Object, obj
{
Object temp = objects[i];
objects[i] = obj;
return temp;
}
public Object getItem(int i) Returns the object in spot i of the arraylist
{
return objects[i];
}
public Object remove(int slot) //Removes the object in spot i of the arraylist and shifts all the elements after i back a space
{
Object temp[] = new Object[ objects.length ];
Object returnItem = objects[slot];
for(int i = 0; i < listSize - 1; i++)
{
if( i < slot )
temp[i] = objects[i];
if( i >= slot )
temp[i] = objects[i + 1];
}
objects = temp;
listSize--;
return returnItem;
}
}